<!DOCTYPE html>
<html>
<head>
<title>编辑优惠券</title>
#include("../include/common.html")
<link href="#(base)/admin/static/plugins/date/bootstrap-datetimepicker.min.css" rel="stylesheet" />
<script src="#(base)/admin/static/plugins/date/bootstrap-datetimepicker.min.js" type="text/javascript"></script>
<script src="#(base)/admin/static/plugins/date/locales/bootstrap-datetimepicker.zh-CN.js" type="text/javascript"></script>
</head>
<body class="gray-bg">
    <div class="wrapper wrapper-content">
        <div class="ibox float-e-margins">
            <div class="ibox-title">
                <h5>编辑优惠券</h5>
            </div>
           <form id="inputForm" action="update" method="post">
           		<input type="hidden" name="id" value="#(coupon.id)">
	            <div class="ibox-content">
	                <div class="form-horizontal clearfix">
	                    <div class="form-group">
                            <label class="col-sm-3 control-label">名称：</label>
                            <div class="col-sm-3">
                                <input class="form-control" type="text" name="name" value="#(coupon.name)">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">优惠金额：</label>
                            <div class="col-sm-3">
                                <input class="form-control" type="text" name="price" value="#(coupon.price)">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">最低消费金额：</label>
                            <div class="col-sm-3">
                                <input class="form-control" type="text" name="miniprice" value="#(coupon.miniprice)">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">有效期开始时间：</label>
                            <div class="col-sm-3">
                                <input class="form-control" type="text" name="startTime" id="startTime" readonly="readonly" value="#(coupon.startTime)">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">有效期结束时间：</label>
                            <div class="col-sm-3">
                                <input class="form-control" type="text" name="endTime" id="endTime" readonly="readonly" value="#(coupon.endTime)">
                            </div>
                        </div>
                        <div class="form-group">
	                        <label class="col-sm-3 control-label">特定分类(使用范围)：</label>
	                        <div class="col-sm-3">
                                <button type="button" class="btn btn-primary" id="productCategorySet">设置</button>
                            </div>
	                    </div>
	                    <div class="form-group">
	                    	<input type="hidden" name="productCategoryIdValue" id="productCategoryIdValue" value="#(coupon.productCategoryIdValue)"/>
	                    	<label class="col-sm-3 control-label">&nbsp;</label>
	                    	<div class="col-sm-3">
			                    <div class="table-responsive">
			                        <table class="table table-bordered" id="productCategoryListTable">
			                            <thead>
			                                <tr>
			                                    <th>ID</th>
			                                    <th>名称</th>
			                                    <th>操作</th>
			                                </tr>
			                            </thead>
			                            <tbody id="productCategoryListTr">
			                            #for(productCategory : coupon.productCategorys)
			                            <tr>
			                            	<td>#(productCategory.id)</td>
			                            	<td>#(productCategory.name)</td>
			                            	<td><a href="javascript:void(0)" class="deleteProductCategory" productCategoryId="#(productCategory.id)">删除</a></td>
			                            </tr>
			                            #end
			                            </tbody>
			                        </table>
			                    </div>
		                    </div>
	                    </div>
	                    <div class="form-group">
	                        <label class="col-sm-3 control-label">特定商品(使用范围)：</label>
	                        <div class="col-sm-3">
                                <button type="button" class="btn btn-primary" id="productSet">设置</button>
                            </div>
	                    </div>
	                    <div class="form-group">
	                    	<input type="hidden" name="productIdValue" id="productIdValue" value="#(coupon.productIdValue)"/>
	                    	<label class="col-sm-3 control-label">&nbsp;</label>
	                    	<div class="col-sm-6">
			                    <div class="table-responsive">
			                        <table class="table table-bordered" id="productListTable">
			                            <thead>
			                                <tr>
			                                    <th>ID</th>
			                                    <th>名称</th>
			                                    <th>图片</th>
			                                    <th>操作</th>
			                                </tr>
			                            </thead>
			                            <tbody id="productListTr">
			                            	#for(product : coupon.products)
				                            <tr>
				                            	<td>#(product.id)</td>
				                            	<td>#(product.name)</td>
				                            	<td><img src="#(imageUrl(product.image))" height="50"/></td>
				                            	<td><a href="javascript:void(0)" class="deleteProduct" productId="#(product.id)">删除</a></td>
				                            </tr>
				                            #end
			                            </tbody>
			                        </table>
			                    </div>
		                    </div>
	                    </div>
	                    <div class="form-group">
	                        <label class="col-sm-3 control-label">说明：</label>
	                        <div class="col-sm-3">
                                <textarea class="form-control" style="height: 100px;" name="remark">#(coupon.remark)</textarea>
                            </div>
	                    </div>
	                </div>
	            </div>
	            <div class="modal-footer col-sm-3 col-sm-offset-2">
	                <button type="submit" class="btn btn-w-m btn-primary">确定</button>
	                <button type="button" class="btn btn-w-m btn-default" onclick="history.back(); return false;">取消</button>
	            </div>
            </form>
        </div>
    </div>
</body>
</html>
<script type="text/javascript">
$().ready(function() {

	var $inputForm = $("#inputForm");
    
    // 表单验证
    $inputForm.validate({
        rules: {
            name: "required"
        }
    });

});
</script>
<script type="text/javascript">

function chooseProductCategoryCall(dataarray){
	var productCategoryIdValue = $("#productCategoryIdValue").val();
	var newproductCategoryIdValue="";
	for(i in dataarray){
		var dataobj = dataarray[i];
		if(productCategoryIdValue.indexOf(","+dataobj.id+",")==-1){
			var html = '<tr><td>'+dataobj.id+'</td><td>'+dataobj.name+'</td><td><a href="javascript:void(0)" class="deleteProductCategory" productCategoryId="'+dataobj.id+'">删除</a></td></tr>'
			$("#productCategoryListTr").append(html);
			newproductCategoryIdValue=newproductCategoryIdValue+dataobj.id+",";
		}
	}
	if(newproductCategoryIdValue!=""){
		if(productCategoryIdValue==""){
			productCategoryIdValue = ","+newproductCategoryIdValue;
		}else{
			productCategoryIdValue = productCategoryIdValue+newproductCategoryIdValue;
		}
	}
	$("#productCategoryIdValue").val(productCategoryIdValue);
}

$("#productCategoryListTr").on("click",".deleteProductCategory",function(){
	var productCategoryId = $(this).attr("productCategoryId");
	var productCategoryIdValue = $("#productCategoryIdValue").val();
	productCategoryIdValue = productCategoryIdValue.replace(","+productCategoryId+",",",");
	$("#productCategoryIdValue").val(productCategoryIdValue);
	$(this).parent().parent().remove();
});

function chooseProductCall(dataarray){
	var productIdValue = $("#productIdValue").val();
	var newproductIdValue="";
	for(i in dataarray){
		var dataobj = dataarray[i];
		if(productIdValue.indexOf(","+dataobj.id+",")==-1){
			var html = '<tr><td>'+dataobj.id+'</td><td>'+dataobj.name+'</td><td><img src="'+dataobj.image+'" height="50"/></td><td><a href="javascript:void(0)" class="deleteProduct" productId="'+dataobj.id+'">删除</a></td></tr>'
			$("#productListTr").append(html);
			newproductIdValue=newproductIdValue+dataobj.id+",";
		}
	}
	if(newproductIdValue!=""){
		if(productIdValue==""){
			productIdValue = ","+newproductIdValue;
		}else{
			productIdValue = productIdValue+newproductIdValue;
		}
	}
	$("#productIdValue").val(productIdValue);
}

$("#productListTr").on("click",".deleteProduct",function(){
	var productId = $(this).attr("productId");
	var productIdValue = $("#productIdValue").val();
	productIdValue = productIdValue.replace(","+productId+",",",");
	$("#productIdValue").val(productIdValue);
	$(this).parent().parent().remove();
});


$("#productCategorySet").on("click",function(){
    $.dialog({ width: '350px',
        height: 500,
        content: 'url:#(base)/admin/coupon/productCategorySet'});
    
   });
   
$("#productSet").on("click",function(){
    $.dialog({ width: '950px',
        height: 500,
        content: 'url:#(base)/admin/coupon/productSet'});
    
   });


    $("#startTime").datetimepicker({
         format: 'yyyy-mm-dd',
            autoclose: true,
            todayBtn: true,
            startView: 'month',
            minView:'year',
            maxView:'decade',
            language:  'zh-CN'
    });
    
    $("#endTime").datetimepicker({
        format: 'yyyy-mm-dd',
           autoclose: true,
           todayBtn: true,
           startView: 'month',
           minView:'year',
           maxView:'decade',
           language:  'zh-CN'
   });
</script>

